package com.xbongbong.saas.util;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 吴峰
 * @date 2022/03/01 13:49
 */
public class StringStreamUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(StringStreamUtil.class);
    /**
     * data最多 1M
     */
    private static final int DATA_MAX_SIZE = 1048576;


    /**
     * 校验data大小
     */
    public static void validateDataSize(String sInputString) throws XbbException {
        if (StringUtil.isNotEmpty(sInputString)){
            try{
                byte[] byteByte = sInputString.getBytes();
                int length = byteByte.length;
                if (length > DATA_MAX_SIZE) {
                    // 大于1M
                    throw new XbbException(ProErrorCodeEnum.API_ERROR_500020);
                }
            } catch (XbbException e) {
                throw e;
            } catch (Exception e){
                LOGGER.error("com.xbongbong.saas.util.StringStreamUtil.validateDataSize error", e);
            }
        }
    }
}
